<?php
class ItemHotForm extends Form
{
	private $cachefile='ItemHot';
	
	function __construct()
	{				
		Form::Form('ItemHotForm');
	}
	
	function on_submit()
	{
		if(User::is_admin())
		{
			$submit 		= Url::get('submit');
			
			if($submit == "Cập nhật" || $submit == "Xóa tất cả")
			{
				$title_offer 	= Url::get('title_offer');
				$item_cats 		= Url::get('item_cats');
					
				$config_update = array(
											'title' 			=> $title_offer,
											'item_cat_offer'	=> array(),
											);
											
				if($submit == "Cập nhật")
				{
					if($item_cats)
					{
						foreach ($item_cats as $catid=>$item_ids)
						{
							if (isset(CGlobal::$allCategories[$catid]) && $item_ids != '')
							{
								$config_update['item_cat_offer'][$catid] = implode(',',explode(',',$item_ids));
							}
						}
					}
				}
				else
				{
					$configs = @CGlobal::$configs['ItemHot']['conf_val'];
					
					$config_update['title'] = $configs['title'];
				}
				
				###########################
					
				if(isset(CGlobal::$configs['ItemHot']))
					DB::update('configs',array("conf_val"=>addslashes(serialize($config_update))),"conf_key='ItemHot'", __FILE__." Dòng:".__LINE__);
				else
					DB::insert('configs',array("conf_key"=>'ItemHot',"conf_val"=>addslashes(serialize($config_update))), __FILE__." Dòng:".__LINE__);
				
				AZLib::get_config(0,1);
			}
			
			
			if($submit == "Cập nhật Quảng cáo")
			{
				$url_detail		=	URL::get('url');
				$status			=	URL::get('status','int',0);
				$type			=	URL::get('type','int',1);
				
				$contentConfig	=	$status."\n".$url_detail."\n".$type;
				
				$banner_file	=	DATA_PATH.'adv/AdvItemHot';
				
				if (isset($_FILES['banner']) && $_FILES['banner']['error']==0){
					$file_ext=strtolower(substr($_FILES['banner']['name'],strlen($_FILES['banner']['name'])-4,4));
					
					if($type==1){//Ảnh
						if(in_array($file_ext,array('.jpg','.gif')))
						{
							if(getimagesize($_FILES['banner']['tmp_name']))
							{	
								if(!move_uploaded_file( $_FILES['banner']['tmp_name'], $banner_file.'.gif'))
								{
									$this->setFormError('banner','Không up load được Banner!');
									return ;
								}
							}
							else
							{
								$this->setFormError('banner','Sai định dạng ảnh!');
								return ;
							}
						}
						else{
							$this->setFormError('banner','Chỉ chấp nhận các định dạng file .jpg , .gif !');
							return ;
						}
					}
					else{
						if(in_array($file_ext,array('.swf')))
						{
							if(!move_uploaded_file( $_FILES['banner']['tmp_name'], $banner_file.'.swf'))
							{
								$this->setFormError('banner','Không up load được Banner!');
								return ;
							}
						}
						else
						{
							$this->setFormError('banner','Flash đại diện chỉ chấp nhận các định dạng file .swf');
							return ;
						}
					}
				}
				
				if(isset(CGlobal::$configs['AdvItemHot']))
					DB::update('configs',array("conf_val"=>$contentConfig),"conf_key='AdvItemHot'");
				else
					DB::insert('configs',array("conf_key"=>'AdvItemHot',"conf_val"=>$contentConfig));
				
				AZLib::get_config(0,1);
				
			}
			
			
			
			if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']){
				$referer = $_SERVER['HTTP_REFERER'];
			}
			else{
				$referer = $_SERVER['REQUEST_URI'];
			}
			
			Url::redirect_url($referer);
		}
		
		Url::redirect_url($_SERVER['REQUEST_URI']);
	}
	
	function draw()
	{
		global $display;
		
		$item_cat_ids 	= '';
		$offer_cats 	= array();
		$item_cats 		= array();
		
		
		$item_cat_offer = array();
		
		$configs = @CGlobal::$configs['ItemHot']['conf_val'];
		
		if($configs)
		{
			$configs = unserialize(stripslashes($configs));
		}
		else
		{
			$configs = array(
								'title' 			=> "SẢN PHẨM HOT",
								'item_cat_offer'	=> array(),
								);
		}
		
		foreach(CGlobal::$subCategories as $top_id => $sub_cats)
		{
			if (isset(CGlobal::$allCategories[$top_id]) && CGlobal::$allCategories[$top_id]['parent_id'] == 0)
			{
				$topcat = CGlobal::$allCategories[$top_id];
           
                if ($topcat['status'] == "HOME")
                {
                	$item_ids = isset($configs['item_cat_offer'][$top_id])?$configs['item_cat_offer'][$top_id]:'';
                	
                	if($item_ids)
					{
						$item_cat_ids .= ($item_cat_ids != '' ? ',' : '').$item_ids;
					}
					
                	$arr = array(
                				'id' 			=> $top_id,
                				'name' 			=> "<a href='" . WEB_DIR . "c{$topcat['id']}/{$topcat['ebname']}'>{$topcat['name']}</a>",
                				'item_ids' 		=> $item_ids,
					             'sub' 			=> array(),
                				'sub_display' 	=> 0,
                				);
                				
                	foreach ($sub_cats as $sid => $subcat)
					{
						$item_ids = isset($configs['item_cat_offer'][$sid])?$configs['item_cat_offer'][$sid]:'';
						
						if($item_ids)
						{
							$arr['sub_display'] = 1;
							
							$item_cat_ids .= ($item_cat_ids != '' ? ',' : '').$item_ids;
						}
						
						$arr['sub'][$sid] = array(
					                				'id' 		=> $sid,
					                				'name' 		=> "<a href='" . WEB_DIR . "c{$subcat['id']}/{$topcat['ebname']}-{$subcat['ebname']}'>{$subcat['name']}</a>",
					                				'item_ids' 	=> $item_ids
					                				);
					}
					
					$item_cat_offer[$top_id] = $arr;
				}
			}
		}
		
		
		if($item_cat_ids)
		{
			$re  = DB::query("SELECT * FROM item WHERE id IN($item_cat_ids) AND status = 1 ORDER BY find_in_set(id,'$item_cat_ids')");
			
			if($re)
			{
				while($product = mysql_fetch_assoc($re))
				{
					if($product['img_url'])
					{
						$product['image'] = AZLib::getImageThumb($product['img_url'],235,150,0,$product['img_server']);
					}
					else
					{    
						$product['image'] = "";//"style/images/no-images-35x35.gif";
					}
					
					if(isset(CGlobal::$allCategories[$product['category_id']])){
						$product['url'] = WEB_DIR.CGlobal::$allCategories[$product['category_id']]['nice_name']."/p{$product['id']}/".AZLib::safe_title($product['name']).".html";
					}
					else{	
						$product['url'] = WEB_DIR."p{$product['id']}/".AZLib::safe_title($product['name']).".html";
					}

					$product['name']  	= stripslashes($product['name']);
					
					$product['price_num'] = $product['price'];
					
					$product['price'] = AZLib::getPrice($product['price'],$product['currency_id']);
					
					if($product['price_out'])
					{
						if($product['price_num'] < $product['price_out'])
						{
							$product['sale_off'] = floor(100 * ($product['price_out'] - $product['price_num'])/$product['price_out']);
						}
						
						$product['price_out'] = AZLib::getPrice($product['price_out'],$product['currency_id']);
					}
					
					//level_1_catid
					//$topcat_id = 
					if(isset($configs['item_cat_offer'][$product['level_1_catid']]) && in_array($product['id'],explode(',',$configs['item_cat_offer'][$product['level_1_catid']])) )
					{
						$offer_cats[$product['level_1_catid']]	  =	CGlobal::$allCategories[$product['level_1_catid']];
						$item_cats[$product['level_1_catid']][$product['id']]	  =	$product;
					}
					
					if(isset($configs['item_cat_offer'][$product['category_id']]) && in_array($product['id'],explode(',',$configs['item_cat_offer'][$product['category_id']])) )
					{
						$offer_cats[$product['category_id']]	  =	CGlobal::$allCategories[$product['category_id']];
						
						$item_cats[$product['category_id']][$product['id']]	  =	$product;
					}
				}
			}
		}
		
		if(User::is_admin())
		{
			$display->add("is_admin",1);
			$display->add("msg",$this->showFormErrorMessages(1));
			$display->add('begin_form',$this->beginForm(true,'post',false,false,1));
			$display->add('end_form',$this->endForm(true));
		}
		
		$display->add('title_offer',$configs['title']);
		
		$display->add('item_cat_offer',$item_cat_offer);
		$display->add('item_cats',$item_cats);
		$display->add('offer_cats',$offer_cats);
		
		
		
		
		
		
		################################################################################################################
		#ADV config
		$config=@CGlobal::$configs['AdvItemHot']['conf_val'];
		
		$status			= 0;
		$url_detail		= '';
		$type_display	= 1;
		
		if($config!='')
		{
			$config_arr=explode("\n",$config);
			
			$status				= (int)$config_arr[0];
			$url_detail			= $config_arr[1];
			$type_display		= (int)$config_arr[2];
		}
			
		if($status)
		{//Bật
			if($type_display == 1)
				$display->add('adv_banner',DATA_PATH.'adv/AdvItemHot.gif');
			else
				$display->add('adv_banner',DATA_PATH.'adv/AdvItemHot.swf');
				
			$display->add('type_display',	$type_display);
			$display->add('url_detail',		$url_detail);
		}
		else
		{//tắt
			$display->add('adv_banner','');
		}
		
		$display->add('adv_status',$status);
		 
		if(User::is_admin())
		{
			$status_arr=array(0=>'Tắt',1=>'Bật');
			$display->add('status_option',AZLib::getOption($status_arr,$status));
			
			$display->add('url',$url_detail);
			
			$type_arr	=   array(1=>'Ảnh',2=>'Flash');
			$type		=	URL::get('type','int',$type_display);
			$display->add('type_option',AZLib::getOption($type_arr,$type));  
		}
		else 
		{
			$display->add('adv_config','');
			$display->add('is_admin',0);
		}

		$display->add('adv_width',200);
		$display->add('adv_height',560);
		#ADV config
		################################################################################################################
		
		
		
		
		$display->output("ItemHot");
	}
}
?>